package com.tywj.lib.core.common.manager

import android.annotation.SuppressLint
import android.app.Activity
import java.util.*

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/7
 *     desc   : Activity管理器
 *     version: 1.0.0
 * </pre>
 */
@SuppressLint("StaticFieldLeak")
object ActivityManager {

    // activity集合（不包括 MainActivity）
    private val activityList = LinkedList<Activity>()
    // 主Activity
    private var mainActivity: Activity? = null

    fun setMainActivity(activity: Activity?) {
        mainActivity = activity
    }

    fun getMainActivity(): Activity? =
        mainActivity

    fun addActivity(activity: Activity) {
        activityList.add(activity)
    }

    fun removeActivity(activity: Activity): Boolean {
        return activityList.remove(activity)
    }

    fun getActivityList(): LinkedList<Activity> =
        activityList

    fun getCurrentActivity(): Activity? {
        if (activityList.size > 0) {
            return activityList.last
        }

        return mainActivity
    }

    fun closeListActivity() {
        for (activity in activityList) {
            activity.finish()
        }
        activityList.clear()
    }

    fun closeAllActivity() {
        closeListActivity()

        mainActivity?.finish()
        mainActivity = null
    }
}